/**
 * OSAmI: Open Source Ambient Intelligence
 * Copyright (C) 2010 Bull S.A.S.
 * Contact: osami-general@osami-commons.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 */

package org.osami.gateway.pseudo.dm;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.logging.Logger;

import org.apache.felix.ipojo.annotations.Bind;
import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Unbind;
import org.osgi.framework.ServiceReference;
import org.osgi.service.device.Constants;
import org.osgi.service.device.Driver;
import org.osgi.service.device.Device;

/**
 * Created by IntelliJ IDEA.
 * User: guillaume
 * Date: 17 juin 2010
 * Time: 13:52:02
 * To change this template use File | Settings | File Templates.
 */
@Component
public class PseudoDeviceManager {
    private static final Logger logger = Logger.getLogger(PseudoDeviceManager.class.getName());

    private Hashtable<String, Driver> drivers;

    public PseudoDeviceManager() {
        this.drivers = new Hashtable<String, Driver>();
    }

    @Bind(optional = true, aggregate = true)
    public void bindDevice(Device device, ServiceReference reference) throws Exception {
        logger.info("PseudoDeviceManager: bindDevice " + device + " " + reference);
        Enumeration<Driver> enumeration = drivers.elements();
        while (enumeration.hasMoreElements()) {
            Driver driver = enumeration.nextElement();
            try {
                int result = driver.match(reference);
                if (result != Device.MATCH_NONE) {
                    driver.attach(reference);
                    logger.info("PseudoDeviceManager: " + reference + " for " + device + " successfully attached to " + driver);
                    break;
                }
                else {
                    logger.info("Driver " + driver + " indicates that it cannot handle the device " + device);
                }
            }
            catch (Exception e) {
                logger.info(e.getMessage());
            }
        }
    }

    @Bind(aggregate = true)
    public void bindDriver(Driver driver, ServiceReference reference) {
        String id = (String) reference.getProperty(Constants.DRIVER_ID);
        logger.info("PseudoDeviceManager: bindDriver " + reference + " for ID " + id);
        drivers.put(id, driver);
    }

    @Unbind
    public void unbindDriver(ServiceReference reference) {
        String id = (String) reference.getProperty(Constants.DRIVER_ID);
        logger.info("PseudoDeviceManager: unbindDriver " + reference + " for ID " + id);
        drivers.remove(id);
    }

}
